# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Simon Sobisch <simonsobisch@gnu.org>

_realname=pdcurses
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
provides=("${MINGW_PACKAGE_PREFIX}-curses")
#replaces=("${MINGW_PACKAGE_PREFIX}-ncurses" "${MINGW_PACKAGE_PREFIX}-termcap")
#conflicts=("${MINGW_PACKAGE_PREFIX}-ncurses" "${MINGW_PACKAGE_PREFIX}-termcap")
pkgver=4.1.0
pkgrel=3
pkgdesc="Curses library on the Win32 API (mingw-w64)"
arch=('any')
url="https://www.projectpluto.com/win32a.htm"
license=('Public Domain')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
options=('staticlibs' 'strip')
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/Bill-Gray/PDCurses/archive/v${pkgver}.tar.gz"
        001-mingw-pdcurses-4.1.0-build.patch
        002-fix-exports.patch
        003-fix-_bool-was-not-declared.patch)
sha256sums=('3421e2e84bdc8220dc6740b70aa9b0e30542064189efc8609e00de78ced75656'
            '913b5aff09d0ab1a2197f66a98657927d85a0dc3577c2b5e69179148fb2b0242'
            '246f93facdd2703f8b9d0bcd57e89688fd861d34a30facc60a48892b330b08bc'
            '6f6875068a13988988b8b04300636db4c4f9f543632597232cb2b64a2fd1218b')

prepare() {
  cd PDCurses-${pkgver}
  patch -p1 -i ${srcdir}/001-mingw-pdcurses-4.1.0-build.patch
  patch -p1 -i ${srcdir}/002-fix-exports.patch
  patch -p1 -i ${srcdir}/003-fix-_bool-was-not-declared.patch
}

build() {
  cd "${srcdir}/PDCurses-${pkgver}"

# NOte that you should use something like -${CARCH}
# to prevent building i686 compiled binaries from
# being compiled with x86_64 compiled binaries and
# vice-versa.  That causes build failures - no surpise.
  cp -rf wingui wingui-shared-${CARCH}
  pushd wingui-shared-${CARCH}
    make -f Makefile.mng \
      CC=${MINGW_PREFIX}/bin/gcc \
      LINK=${MINGW_PREFIX}/bin/gcc \
      STRIP=${MINGW_PREFIX}/bin/strip \
      AR=${MINGW_PREFIX}/bin/ar \
      WIDE=Y \
      UTF8=Y \
      DLL=Y
  popd

  cp -rf wingui wingui-static-${CARCH}
  pushd wingui-static-${CARCH}
    make -f Makefile.mng \
      CC=${MINGW_PREFIX}/bin/gcc \
      LINK=${MINGW_PREFIX}/bin/gcc \
      STRIP=${MINGW_PREFIX}/bin/strip \
      AR=${MINGW_PREFIX}/bin/ar \
      WIDE=Y \
      UTF8=Y
  popd
  
}

package() {
  cd "${srcdir}/PDCurses-${pkgver}"

  mkdir -p ${pkgdir}${MINGW_PREFIX}/{bin,include,lib}
  mkdir ${pkgdir}${MINGW_PREFIX}/include/pdcurses
 
  install wingui-shared-${CARCH}/*.exe ${pkgdir}${MINGW_PREFIX}/bin/
  install wingui-shared-${CARCH}/libpdcurses.dll ${pkgdir}${MINGW_PREFIX}/bin/
  install wingui-shared-${CARCH}/libpdcurses.dll.a ${pkgdir}${MINGW_PREFIX}/lib/libpdcurses.dll.a
  install wingui-static-${CARCH}/libpdcurses.a ${pkgdir}${MINGW_PREFIX}/lib/libpdcurses.a

  echo '#include "pdcurses/curses.h"' > pdcurses.h
  install -m 0644 curses.h panel.h term.h acs_defs.h ${pkgdir}${MINGW_PREFIX}/include/pdcurses/
  install -m 0644 pdcurses.h ${pkgdir}${MINGW_PREFIX}/include/pdcurses.h
}
